<?php
App::uses('AppController', 'Controller');
/**
 * Galleries Controller
 *
 * @property Gallery $Gallery
 */
class GalleriesController extends AppController {

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Gallery->recursive = 1;
		$galleries = $this->Gallery->find('all', array('order' => array('name')));
		$this->set('galleries', $galleries);
		if ($this->request->is('post')) {
			$this->Gallery->create();
			$this->request->data['Gallery']['created_at'] = date("Y-m-d H:i:s");
      $this->request->data['Gallery']['created_by'] = $this->Auth->user('id');
			if ($this->Gallery->save($this->request->data)) {
				$this->Session->setFlash(__('The gallery has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'default', array(), 'bad');
			}

			if(isset($this->request->data['Gallery'])){
				$this->set('show_form', true);
			}
		}

	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->Gallery->recursive = 1;
		if (!$this->Gallery->exists($id)) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		$options = array('conditions' => array('Gallery.' . $this->Gallery->primaryKey => $id));
		$gallery = $this->Gallery->find('first', $options);
		$this->set('gallery', $gallery);
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Gallery->create();
			$this->request->data['Gallery']['created_at'] = date("Y-m-d H:i:s");
		        $this->request->data['Gallery']['created_by'] = $this->Auth->user('id');
			if ($this->Gallery->save($this->request->data)) {
				$this->Session->setFlash(__('The gallery has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'));
			}
		}
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Gallery->exists($id)) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Gallery->save($this->request->data)) {
				$this->Session->setFlash(__('The gallery has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Gallery.' . $this->Gallery->primaryKey => $id));
			$this->request->data = $this->Gallery->find('first', $options);
		}
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Gallery->id = $id;
		if (!$this->Gallery->exists()) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Gallery->delete()) {
			$this->Session->setFlash(__('Gallery deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Gallery was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

/**
* add image to gallery
*/
	public function admin_add_image(){
		if ($this->request->is('post')) {
			$id = $this->request->data['Gallery']['id'];
			if ($this->Gallery->exists($id)){	
				$this->loadModel('Image');
				$this->Image->updateMeioUploadSize('Gallery', null);
				if ($this->Gallery->saveAssociated($this->request->data)) {
			                $this->Session->setFlash(__('The photo has been saved'), 'default', array(), 'good');
			        } else {
			                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'default', array(), 'bad');
			        }
		
			}
		}
		$this->redirect(array('action' => 'view', $id));
	}

/**
* delete image
*/
	public function admin_delete_img($gallery_id = null, $img_id = null) {
		$this->loadModel('Image');
		$this->Image->updateMeioUploadSize('Gallery', null);
		if ($this->request->is('post')) {
				$this->Image->id = $img_id;
				if (!$this->Image->exists()) {
					throw new NotFoundException(__('Invalid image'));
				}
				$this->request->onlyAllow('post', 'delete');
				if ($this->Image->delete()) {
					$this->Session->setFlash(__('Image deleted'), 'default', array(), 'good');
				}else{
					$this->Session->setFlash(__('Gallery was not deleted'), 'default', array(), 'bad');
				}
		}
		$this->redirect(array('action' => 'view', $gallery_id));
	}

/**
*	browse images
**/
	public function admin_browser_images(){
		$this->Gallery->recursive = 0;
		$galleries = $this->Gallery->find('all', array('order' => array('name')));
		$this->set('galleries', $galleries);

		$this->layout = 'ajax';
	}

/**
*	load images
**/
	public function admin_load_images($id = null){
		if ($this->Gallery->exists($id) && $this->request->is('post')) {
			$this->Gallery->recursive = 1;
			$options = array('conditions' => array('Gallery.' . $this->Gallery->primaryKey => $id));
			$gallery = $this->Gallery->find('first', $options);
			$this->set('gallery', $gallery);
		}
		$this->layout = 'ajax';
	}

}
